﻿using System;
using System.Configuration;
using System.Linq;
using Cpuss.Strategies;
using Cpuss.Tests.Properties;
using NUnit.Framework;

namespace Cpuss.Tests
{

    /// <summary>
    /// Tests for Logger.
    /// </summary>
    [TestFixture]
    public class LoggerTest
    {

        /// <summary>
        /// Test to make sure that the connection string is correct.
        /// </summary>
        [Test]
        public void ConnectionStringTest()
        {
            Logger actual = new Logger(ConfigurationManager.ConnectionStrings["CpussTestConn"].ConnectionString);

            Assert.AreEqual(Resources.LoggingTestConnectionString, actual.ConnectionString);
        }

        /// <summary>
        /// Check to see that the runner was added correctly to the database.
        /// </summary>
        [Test]
        public void AddRunnerTest()
        {
            Runner runner = new Runner(10, 20, 20, new RoundRobin(10));
            Logger logger = new Logger(ConfigurationManager.ConnectionStrings["CpussTestConn"].ConnectionString);

            runner.Run();
            logger.Add(runner);
            logger.SubmitData();

            using (CpussDataContext actual = new CpussDataContext(ConfigurationManager.ConnectionStrings["CpussTestConn"].ConnectionString))
            {
                Assert.AreEqual(1, actual.Runs.Where(x => x.Id == runner.Id).Count());
                actual.Runs.DeleteOnSubmit(actual.Runs.Where(x => x.Id == runner.Id).First());
                actual.SubmitChanges();
            }
        }

        /// <summary>
        /// Check to see that the correct excepotion is raised when adding a null runner to the logger.
        /// </summary>
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void RunnerNullTest()
        {
            Runner runner = null;
            Logger logger = new Logger(ConfigurationManager.ConnectionStrings["CpussTestConn"].ConnectionString);

            logger.Add(runner);
            logger.SubmitData();
        }

    }

}
